import axios from 'axios'

// 创建axios实例 - 开发环境使用代理，生产环境使用环境变量
const getBaseURL = (envVar, fallback) => {
  if (import.meta.env.MODE === 'development') {
    return fallback // 开发环境使用代理路径
  }
  return import.meta.env[envVar] || fallback
}

const server = axios.create({
  baseURL: getBaseURL('VITE_VIDEO_API', '/videoapi'),
  timeout: 10000
})

const guobiao = axios.create({
  baseURL: getBaseURL('VITE_GUOBIAO_API', '/gbapi'),
  timeout: 10000
})

// 响应拦截器 - server
server.interceptors.response.use(
  ({ data }) => {
    if (data.code === 0 || data.code === 200) {
      return data
    } else {
      return Promise.reject(data.msg || '请求失败')
    }
  },
  (err) => {
    if (err.code === 'ECONNABORTED') {
      return Promise.reject('请求超时，请稍后重试')
    }

    if (err.response) {
      const { statusText } = err.response
      return Promise.reject(statusText)
    } else {
      return Promise.reject('网络错误或服务器未响应')
    }
  }
)

// 响应拦截器 - guobiao
guobiao.interceptors.response.use(
  ({ data }) => {
    if (data.code === 0) {
      return data
    } else {
      return Promise.reject(data.msg || '请求失败')
    }
  },
  (err) => {
    if (err.response) {
      const { statusText } = err.response
      return Promise.reject(statusText)
    }
    return Promise.reject('网络错误')
  }
)

// 设备管理API
export class DeviceController {
  prefix = "/api/device"

  // 获取设备列表
  getDeviceList(data) {
    return guobiao({
      url: this.prefix + "/list",
      method: "POST",
      data: data
    })
  }

  // 添加设备
  addDevice(info) {
    return guobiao({
      url: this.prefix + "/add",
      method: "POST",
      data: info
    })
  }

  // 编辑设备
  editDevice(info) {
    return guobiao({
      url: this.prefix + "/config",
      method: "POST",
      data: info
    })
  }

  // 删除设备
  removeDevice(info) {
    return server({
      url: this.prefix + "/delDevice",
      method: "POST",
      data: info
    })
  }

  // 获取所有通道信息
  getChannelAll(page) {
    return server({
      url: this.prefix + "/getChannelAll",
      method: "POST",
      data: page
    })
  }

  // 获取通道列表
  getChannelList({ deviceId, count, page }) {
    return guobiao({
      url: this.prefix + "/channel",
      method: "POST",
      data: { deviceId, showNums: count, pageNum: page }
    })
  }

  // 编辑通道
  editChannel(info) {
    return guobiao({
      url: this.prefix + "/channel/config",
      method: "POST",
      data: info
    })
  }

  // 删除通道
  removeChannel(info) {
    return guobiao({
      url: this.prefix + "/channel/del",
      method: "POST",
      data: info
    })
  }
}

// 报警管理API
export class WarnController {
  prefix = "/api/algo"

  // 删除报警数据
  delete(list) {
    return server({
      url: this.prefix + '/deleteAlarmData',
      method: "POST",
      data: { eventId: list }
    })
  }

  // 获取报警信息
  list({ startTime, endTime, showNums, pageNum, deviceId, channelId, algoType }) {
    return server({
      url: this.prefix + '/queryAlarmData',
      method: "POST",
      data: {
        algoType,
        channelId,
        deviceId,
        startTime,
        endTime,
        showNums,
        pageNum,
      }
    })
  }
}

// 算法模型API
export class ModelController {
  prefix = "/api/model"

  // 获取算法模型列表
  getModel(data) {
    return server({
      url: this.prefix + "/list",
      method: "POST",
      data
    })
  }
}

// 通道管理API
export class ChannelController {
  prefix = "/api/channel"

  // 获取通道的算法参数
  getChannelAlgo(data) {
    return server({
      url: this.prefix + "/getChannelAlgo",
      method: "POST",
      data
    })
  }

  // 设置相机参数
  setChannelAlgo(data) {
    return server({
      url: this.prefix + "/setChannelAlgo",
      method: "POST",
      data
    })
  }

  // 获取图片
  getImage({ deviceId, channelId }) {
    return server({
      url: this.prefix + "/getChannelImg",
      method: "POST",
      data: { deviceId, channelId }
    })
  }
}

// 媒体流API
export class MediaController {
  prefix = "/api/media"

  // 视频预览
  getStream(data) {
    return server({
      url: this.prefix + '/getStreamUrl',
      method: "POST",
      data
    })
  }
}

// 配置管理API
export class ConfigController {
  prefix = "/api"

  // 获取版本信息
  getVersion() {
    return server({
      url: '/api/getVersion',
      method: "GET",
      timeout: 1000
    })
  }
}

// 导出实例
export const deviceController = new DeviceController()
export const warnController = new WarnController()
export const modelController = new ModelController()
export const channelController = new ChannelController()
export const mediaController = new MediaController()
export const configController = new ConfigController()
